package logic

import (
	"context"
	"jd-mall/jd-srv/model"

	"jd-mall/jd-srv/internal/svc"
	"jd-mall/jd-srv/jd"

	"github.com/zeromicro/go-zero/core/logx"
)

type ShopListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewShopListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ShopListLogic {
	return &ShopListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商品列表
func (l *ShopListLogic) ShopList(in *jd.ShopListRequest) (*jd.ShopListResponse, error) {
	// todo: add your logic here and delete this line
	switch {
	case in.Size > 100:
		in.Size = 100
	case in.Size <= 0:
		in.Size = 10
	}

	offset := (in.Page - 1) * in.Size
	var shop []model.Shop
	model.DB.Offset(int(offset)).Limit(int(in.Size)).Find(&shop)
	var shops []*jd.Lists
	for _, m := range shop {
		list := jd.Lists{
			Name:  m.Name,
			Price: int64(m.Price),
			Num:   int64(m.Num),
			Stock: int64(m.Stock),
			Img:   m.Img,
			State: int64(m.State),
		}
		shops = append(shops, &list)
	}
	return &jd.ShopListResponse{
		List: shops,
	}, nil
}
